package conf2.ui;

import conf2.model.ESpecialCard;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import javax.swing.BorderFactory;
import javax.swing.JComponent;

public class SpecialCardWidget extends JComponent implements MouseListener {

    private BufferedImage enabledImage,  disabledImage;
    private ImagePanel enabledComponent,  disabledComponent;
    private boolean isEnabled = true;
    private Listener listener;
    private boolean isResponsive = true;
    private boolean hasBeenPlayed = false;
    private String enabledToolTip;
    private ESpecialCard card;
    public static final int CARD_WIDTH = Images.getSpecialCard(ESpecialCard.A_KING_REVEALED).getWidth();
    public static final int CARD_HEIGHT = Images.getSpecialCard(ESpecialCard.A_KING_REVEALED).getHeight();

    public static interface Listener {

        void specialCardClicked(ESpecialCard which);
    }

    public SpecialCardWidget(ESpecialCard card, String enabledToolTip,
            String disabledToolTip, Listener listener) {
        this.listener = listener;
        this.card = card;
        enabledImage = Images.getSpecialCard(card);

        disabledImage = new BufferedImage(enabledImage.getWidth(), enabledImage.getHeight(), BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = (Graphics2D) disabledImage.getGraphics();
        g.drawImage(enabledImage, 0, 0, null);

        g.setColor(new Color(0, 0, 0, 192));
        g.fillRect(0, 0, disabledImage.getWidth(), disabledImage.getHeight());

        Color borderColor = new Color(200, 200, 200);

        setLayout(null);
        enabledComponent = new ImagePanel(enabledImage);
        enabledComponent.setToolTipText(enabledToolTip);
        enabledComponent.setLocation(0, 0);
        enabledComponent.addMouseListener(this);
        enabledComponent.setBorder(BorderFactory.createLineBorder(borderColor, 1));
        enabledComponent.setHoverSensitive(true);
        add(enabledComponent);

        this.enabledToolTip = enabledToolTip;

        disabledComponent = new ImagePanel(disabledImage);
        disabledComponent.setToolTipText(disabledToolTip);
        disabledComponent.setLocation(0, 0);
        disabledComponent.setBorder(BorderFactory.createLineBorder(borderColor, 1));
        disabledComponent.setHoverSensitive(false);
        add(disabledComponent);

        enabledComponent.setVisible(isEnabled);
        disabledComponent.setVisible(!isEnabled);

        setSize(enabledComponent.getSize());
        setPreferredSize(getSize());

        addMouseListener(this);

    }

    @Override
    public void setEnabled(boolean b) {
        isEnabled = b;
        enabledComponent.setVisible(b);
        disabledComponent.setVisible(!b);
    }

    public boolean hasBeenPlayed() {
        return hasBeenPlayed;
    }

    @Override
    public boolean isEnabled() {
        return isEnabled;
    }

    public void mousePressed(MouseEvent e) {
        if (isResponsive && isEnabled) {
            listener.specialCardClicked(card);
        }
    }

    public boolean isResponsive() {
        return isResponsive;
    }

    public void setResponsive(boolean isResponsive) {
        this.isResponsive = isResponsive;
    }

    public void mouseClicked(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }

    String getEnabledTooltipText() {
        return enabledToolTip;
    }
}
